package handler

import (
	"context"
	"encoding/json"
	"fmt"
	"github.com/gin-gonic/gin"
	"net/http"
	"time"
	"video_08_01/api/request"
	"video_08_01/global"
	"video_08_01/internal/model"
)

func AddBarrage(c *gin.Context) {

}

func ListBarrage(c *gin.Context) {
	var req request.ListBarrageForm
	var barrage model.Barrage

	err := c.ShouldBind(&req)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code":    100000,
			"message": "验证失败",
			"data":    "",
		})
		return
	}

	listBarrage, err := barrage.ListBarrage()
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code":    100000,
			"message": "加载失败",
			"data":    "",
		})
		return
	}

	marshal, err := json.Marshal(listBarrage)
	if err != nil {
		return
	}

	global.RDB.Set(context.Background(), fmt.Sprintf("barrage:%d", req.VideoId), marshal, 3*(24*time.Hour))

	c.JSON(http.StatusOK, gin.H{
		"code":    http.StatusOK,
		"message": "加载成功",
		"data":    listBarrage,
	})

}
